package com.atguigu.msm.controller;

import com.atguigu.commonutils.R;
import com.atguigu.msm.component.SmsComponent;
import com.atguigu.msm.utils.RandomUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
//@CrossOrigin
@RequestMapping("/edumsm")
public class MsmController {

    @Autowired
    private SmsComponent smsComponent;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @GetMapping("sendcode/{phone}")
    public R sendCode(@PathVariable("phone")String phone){
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return R.ok();
        }
        code= RandomUtil.getFourBitRandom();
        smsComponent.sendSmsCode(phone,code);
        //将短信发送的手机号以及验证码存储到redis中
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        return R.ok();
    }
}
